package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessQandA;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.HQDetailsVO;
import com.digitalchina.lxnsy.entity.HelpAndSupportVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.viewholder.HelpAndSupportHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/3/2 15:40
 * 修改备注：
 * ================================================
 */
public class HelpAndSupportActivity extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;
    @Bind(R.id.help_and_support_ll_manual_operation)
    LinearLayout helpAndSupportLlManualOperation;
    @Bind(R.id.help_and_support_tv_question)
    TextView helpAndSupportTvQuestion;
    @Bind(R.id.help_and_support_tv_answer)
    TextView helpAndSupportTvAnswer;
    @Bind(R.id.layout_advisory_history)
    LinearLayout layoutAdvisoryHistory;
    @Bind(R.id.view_line_advisory)
    View viewLineAdvisory;
    @Bind(R.id.layout_similar_question)
    LinearLayout layoutSimilarQuestion;


    private RecyclerArrayAdapter adapter;
    private int page = 1;
    private int limit = 10;
    private String keyWord = "";
    private Map<String, Object> map;

    private HelpAndSupportVO supportVO;
    private List<HQDetailsVO> hqListsVOList = new ArrayList<>();

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_help_and_support_new);
        setTitle("帮助与支持");
    }

    @Override
    public void initMember() {

        map = (Map<String, Object>) getIntent().getSerializableExtra("map");
        keyWord = map.get("keyWord").toString();
        initRecyclerView();
    }

    private void initRecyclerView() {


        recyclerView.setLayoutManager(new LinearLayoutManager(this));


//        recyclerView.addItemDecoration(Utils.setDividerDecoration(this, 1.0f, 15.0f, 15.0f));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new HelpAndSupportHolder(parent);
            }
        });

        onRefresh();
    }

    @Override
    public void addListener() {
        recyclerView.setRefreshListener(this);

        adapter.setMore(R.layout.view_more, this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                HQDetailsVO hqDetailsVO = (HQDetailsVO) adapter.getAllData().get(position);
                map.put("HADetailsVO", hqDetailsVO);
//                GotoUtil.gotoActivity(HelpAndSupportActivity.this, IssueDetailActivity.class, "map", map);
                GotoUtil.gotoActivity(HelpAndSupportActivity.this, AdvisoryHistoryDetailsActivity.class, "map", map);
            }
        });

        helpAndSupportLlManualOperation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GotoUtil.gotoActivity(HelpAndSupportActivity.this, ChooseExpertsListActivity.class, "map", map);
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_GET_SIMILAR_QUESTION_LIST_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_SIMILAR_QUESTION_LIST_FIELD:
                        if (adapter.getAllData().size() > 0)
                            adapter.pauseMore();
                        else
                            recyclerView.showEmpty();
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

        supportVO = FastJsonUtil.getBean(object.toString(), "body", "similarQuestion", HelpAndSupportVO.class);
        hqListsVOList = FastJsonUtil.getListBean(object.toString(), "body", "hisSimilarQuestion", HQDetailsVO.class);

        if (null == hqListsVOList || hqListsVOList.size() <= 0 && null == supportVO) { // 如果相似问题、咨询历史为空 则直接跳转至咨询专家
            showToast("未找到相似问题, 请直接咨询专家");
            GotoUtil.gotoActivity(HelpAndSupportActivity.this, ChooseExpertsListActivity.class, true, "map", map);
            return;
        }

        setContent();

        if (null == hqListsVOList || hqListsVOList.size() <= 0) {
            layoutAdvisoryHistory.setVisibility(View.GONE);
            viewLineAdvisory.setVisibility(View.GONE);
            recyclerView.setVisibility(View.GONE);
        }

        if (page == 1) { // 刷新
            adapter.clear();
        }

        if (hqListsVOList != null) {
            if (hqListsVOList.size() < limit) {
                adapter.stopMore();
            }
        }

        adapter.addAll(hqListsVOList);
    }

    /**
     * 设置相似问题及答案数据
     */

    private void setContent() {
        if (null == supportVO) //相似问题为空
            layoutSimilarQuestion.setVisibility(View.GONE);
        else {
            helpAndSupportTvQuestion.setText(TextUtils.isEmpty(supportVO.getTitle()) ? "" : supportVO.getTitle());
            helpAndSupportTvAnswer.setText(TextUtils.isEmpty(supportVO.getContent()) ? "" : supportVO.getContent());
        }
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onRefresh() {
        page = 1;
        BusinessQandA.getsimilarquestionlist(this, String.valueOf(page), String.valueOf(limit), keyWord, mHandler);
    }

    @Override
    public void onLoadMore() {
            page++;
            BusinessQandA.getsimilarquestionlist(this, String.valueOf(page), String.valueOf(limit), keyWord, mHandler);
    }
}
